<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="" prop="setmId">
        <el-select v-model="queryParams.setmId" placeholder="请选择套餐">
          <el-option :disabled="true" v-for="item in setmealOptions" :key="item.setmId" :label="item.name"
            :value="item.setmId" />
        </el-select>
      </el-form-item>
      <el-form-item label="" prop="depId">
        <el-select v-model="queryParams.depId" placeholder="请选择科室" @change="parentQuery('queryDept')" clearable>
          <el-option v-for="item in depOptions" :key="item.depId" :label="item.depName" :value="item.depId" />
        </el-select>
      </el-form-item>
      <el-form-item label="" prop="proId">
        <el-select v-model="queryParams.proId" placeholder="请选择检查项" @change="childQuery('queryCheck')" clearable>
          <el-option v-for="item in checkOptions" :key="item.proId" :label="item.checkName" :value="item.proId" />
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="项目费用" prop="checkCost">
        <el-input v-model="queryParams.checkCost" placeholder="请输入项目费用" clearable @keyup.enter.native="handleQuery" />
      </el-form-item> -->
      <el-form-item label="" prop="checkType">
        <el-select v-model="queryParams.checkType" placeholder="请选择项目类型" clearable>
          <el-option v-for="dict in dict.type.health_examination_type" :key="dict.value" :label="dict.label"
            :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['jktj:checkgroup:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['jktj:checkgroup:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['jktj:checkgroup:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['jktj:checkgroup:export']">导出</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="info" plain icon="el-icon-close" size="mini" @click="handleClose">关闭</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table border v-loading="loading" :data="checkgroupList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column type="index" width="55" align="center" />
      <!-- <el-table-column label="id" align="center" prop="id" /> -->
      <!-- <el-table-column label="体检套餐" align="center" prop="jktjSetmeal.name" width="200" /> -->

      <el-table-column label="检查项目" align="center" prop="jktjCheck.checkName" />
      <el-table-column label="计量单位" align="center" prop="checkUnit" />
      <el-table-column label="所属科室" align="center" prop="jktjDepartment.depName" />
      <el-table-column label="项目费用(￥)" align="center" prop="checkCost" />
      <el-table-column label="项目类型" align="center" prop="checkType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.health_examination_type" :value="scope.row.checkType" />
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="250">
        <template slot-scope="scope">
          <el-button size="small" type="success" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['jktj:checkgroup:edit']">修改</el-button>
          <el-button size="small" type="danger" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['jktj:checkgroup:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改套餐项目对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="320px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="体检套餐" prop="setmId">
          <el-select :disabled="disabledSetmeal" v-model="form.setmId" placeholder="请选择套餐">
            <el-option v-for="item in setmealOptions" :key="item.setmId" :label="item.name" :value="item.setmId" />
          </el-select>
        </el-form-item>
        <el-form-item label="检测科室" prop="depId">
          <el-select v-model="form.depId" placeholder="请选择科室" @change="parentQuery('addDept')">
            <el-option v-for="item in depOptions" :key="item.depId" :label="item.depName" :value="item.depId" />
          </el-select>
        </el-form-item>
        <el-form-item label="检查项目" prop="proId">
          <el-select v-model="form.proId" placeholder="请选择检查项" @change="childQuery('addCheck')">
            <el-option v-for="item in checkOptions" :key="item.proId" :label="item.checkName" :value="item.proId" />
          </el-select>
        </el-form-item>
        <el-form-item label="计量单位" prop="checkUnit">
          <el-input v-model="form.checkUnit" placeholder="请输入计量单位" clearable />
        </el-form-item>
        <el-form-item label="项目费用" prop="checkCost">
          <el-input v-model="form.checkCost" placeholder="请输入项目费用" clearable />
        </el-form-item>
        <el-form-item label="项目类型" prop="checkType">
          <el-select v-model="form.checkType" placeholder="请选择项目类型">
            <el-option v-for="dict in dict.type.health_examination_type" :key="dict.value" :label="dict.label"
              :value="dict.value"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
// 体检套餐检查项接口
import { listCheckgroup, getCheckgroup, delCheckgroup, addCheckgroup, updateCheckgroup } from "@/api/jktj/checkgroup";
// 套餐接口
import { listSetmeal, getSetmeal, updateSetmeal } from "@/api/jktj/setmeal";
// 科室接口
import { listDepartment } from "@/api/jktj/department";
// 检查项接口
import { listCheck } from "@/api/jktj/check";

export default {
  name: "Checkgroup",
  dicts: ['health_examination_type'],
  data () {
    return {
      totalCost: 0,

      // 禁用套餐选择
      disabledSetmeal: true,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 套餐项目表格数据
      checkgroupList: [],
      // 套餐选择列表
      setmealOptions: [],
      // 科室选择列表
      depOptions: [],
      // 检查项选择列表
      checkOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        setmId: null,
        depId: null,
        proId: null,
        checkCost: null,
        checkType: null,
      },
      query: {
        depId: null,
        proId: null,
      },
      // 表单参数
      form: {},
      setmealForm: {
        setmId: null,
        price: null,
      },
      // 表单校验
      rules: {
        depId: [
          { required: true, message: "科室不能为空", trigger: "change" }
        ],
        proId: [
          { required: true, message: "检查项不能为空", trigger: "change" }
        ],
      }
    };
  },
  created () {
    // this.getList();
    const setmId = this.$route.params && this.$route.params.setmId;
    this.getSetmealInfo(setmId);
    // console.log(this.totalCost);
    this.getAllSetmealList();
    this.getAllDepList();
  },
  mounted () {
    // const columnValues = this.getColumnValues();
    // console.log(columnValues);
  },
  computed: {

  },
  methods: {
    // 获取表格某列的所有值
    getColumnValues () {
      const columnValues = [];
      this.checkgroupList.forEach(row => {
        columnValues.push(row.checkCost);
      });
      return columnValues;
    },
    //关联搜索
    parentQuery: function (param) {
      if (param == 'queryDept') {
        this.getAllCheckList(this.queryParams);
      }
      if (param == 'addDept') {
        // this.query = {
        //   depId: null,
        //   proId: null,
        // }
        this.query.depId = this.form.depId
        // this.getAllCheckList(this.query);
        listCheck(this.query).then(response => {
          this.checkOptions = response.rows;
          // console.log(this.checkOptions)
          if (this.checkOptions.length != 0) {
            this.form.proId = this.checkOptions[0].proId;
          } else {
            this.form.proId = null;
          }

        })
      }
    },
    childQuery: function (param) {
      if (param == 'queryCheck') {

      }
      if (param == 'addCheck') {

      }
    },
    // 根据科室id获取所有检查项
    getAllCheckList (Params) {
      listCheck(Params).then(response => {
        this.checkOptions = response.rows;
        // console.log(this.checkOptions)
        if (this.checkOptions.length != 0) {
          this.queryParams.proId = this.checkOptions[0].proId;
        } else {
          this.queryParams.proId = null;
        }

      })
    },
    // 获取所有科室列表
    getAllDepList () {
      listDepartment().then(response => {
        this.depOptions = response.rows;
      })
    },
    // 获取套餐详情
    getSetmealInfo (setmId) {
      getSetmeal(setmId).then(response => {
        this.queryParams.setmId = response.data.setmId;
        this.getList();
      })
    },
    // 获取所有体检套餐列表
    getAllSetmealList () {
      listSetmeal().then(response => {
        this.setmealOptions = response.rows;
      })
    },
    /** 查询套餐项目列表 */
    getList () {
      this.loading = true;
      listCheckgroup(this.queryParams).then(response => {
        this.checkgroupList = response.rows;
        this.total = response.total;
        this.loading = false;
        let sum = 0;
        for (var i = 0; i < this.checkgroupList.length; i++) {
          sum += this.checkgroupList[i].checkCost;
        }
        this.totalCost = sum;
        const setmId = this.$route.params && this.$route.params.setmId;
        this.setmealForm.setmId = setmId;
        this.setmealForm.price = this.totalCost;
        updateSetmeal(this.setmealForm).then(response => {
          // console.log(this.setmealForm.price)
        })
      });
    },
    /** 返回按钮操作 */
    handleClose () {
      const obj = { path: "/examine/setmeal" };
      this.$tab.closeOpenPage(obj);
    },
    // 取消按钮
    cancel () {
      this.open = false;
      this.reset();
      this.queryParams.proId = null;
    },
    // 表单重置
    reset () {
      this.form = {
        id: null,
        setmId: null,
        depId: null,
        proId: null,
        checkCost: 0.00,
        checkType: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        checkUnit: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery () {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery () {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange (selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd () {
      this.reset();
      this.open = true;
      this.title = "添加体检套餐项目";
      const setmId = this.$route.params && this.$route.params.setmId
      getSetmeal(setmId).then(response => {
        this.form.setmId = response.data.setmId;
      })
    },
    /** 修改按钮操作 */
    handleUpdate (row) {
      this.reset();
      const id = row.id || this.ids
      getCheckgroup(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改体检套餐项目";
      });
      this.getAllCheckList(this.form);
    },
    /** 提交按钮 */
    submitForm () {

      this.$refs["form"].validate(valid => {
        let checkCost = parseFloat(this.form.checkCost)
        // this.totalCost += checkCost;
        this.queryParams.proId = null;
        if (valid) {
          if (this.form.id != null) {
            updateCheckgroup(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCheckgroup(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
      // console.log(this.totalCost)
    },
    /** 删除按钮操作 */
    handleDelete (row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除该套餐项目？').then(function () {
        return delCheckgroup(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport () {
      this.download('jktj/checkgroup/export', {
        ...this.queryParams
      }, `checkgroup_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
